package com.CGFrame.struts.bean.material;

import java.io.File;
import java.util.Date;
import java.util.LinkedList;

import com.CGFrame.struts.POJOs.Material;
import com.CGFrame.struts.POJOs.Materialgroup;
import com.CGFrame.struts.POJOs.Reguser;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.config.GenericConfig;

public class MaterialBean {
	private Material material;
	private static String materialBase = GenericConfig.getInstance().getMaterial();	

	public MaterialBean(int materialId)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		material =(Material)pop.UniqueQuery("Material","materialId,"+materialId);
	}
	public MaterialBean(Material material)
	{
		this.material = material;
	}
	public MaterialBean(String regUserId, Materialgroup materialgroup,
			String materialName, String keyWord, String description,
			Date inputTime)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Reguser reguser = (Reguser)pop.load("Reguser", Integer.parseInt(regUserId) );
		material = new Material(reguser, materialgroup,
				 materialName,  keyWord,  description,
				 inputTime);
		pop.saveObject(material);
		getAbPath();
	}
	
	/**
	 * make or get material path
	 * @return File path
	 */
	public File getAbPath() {
		StringBuffer path = new StringBuffer(materialBase);
		LinkedList<String> list =  this.getMaterialGroupNameList();
		while ( !(list.isEmpty()) )
		{
			path.append("/").append(list.poll());
		}
		path.append("/").append(material.getMaterialName());
		return mkdictory(path.toString());
		
	}
	
	public String getRePath()
	{
		StringBuffer path = new StringBuffer();
		LinkedList<String> list =  this.getMaterialGroupNameList();
		while ( !(list.isEmpty()) )
		{
			path.append(list.poll()).append("/");
		}
		path.append(material.getMaterialName()).append("/");
		return path.toString();
	}
	private File mkdictory(String fileName) {
		File file = new File(fileName);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}
	
	public LinkedList<String> getMaterialGroupNameList()
	{
		LinkedList<String> list = new LinkedList<String>();
		Materialgroup mg = material.getMaterialgroup();
		while(mg!=null)
		{
			list.addFirst(mg.getMaterialGroupName());
			mg = mg.getMaterialgroup();
		}
		return list;
	}
	public String getName() {
		
		return material.getMaterialName();
	}
	public Material getMaterial() {
		
		return material;
	}
	public String delete()
	{
		String name = material.getMaterialName();
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		pop.delete(material);
		return name;
	}
	public void update(String description ,String keyWord)
	{
		if (description!=null && !(description.equals("")) )    
			material.setDescription(description);
		if (keyWord!=null && !(keyWord.equals("")) )
			material.setKeyWord(keyWord);
		
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		pop.saveObject(material);
	}
	public boolean isWanted(String[] keys)
	{
		

		StringBuffer keyBuffer = new StringBuffer();
		keyBuffer.append(material.getMaterialName()).append(" ").append(material.getKeyWord()).append(" ");
		
		LinkedList<String> keyList = this.getMaterialGroupNameList();
		int size = keyList.size();
		for(int i=0;i<size;i++)
		{
			keyBuffer.append(keyList.get(i)).append(" ");
		}
		String  keyLower = keyBuffer.toString().toLowerCase(); // 英文小写
		int length = keys.length;
		if (length==0) return false; // 输入"  "是长度为0
		for(int j=0;j<length;j++)
		{
			if ( keyLower.indexOf(keys[j].toLowerCase())==-1) //不包含某个关键字
			{
				return false;
			}
			
		}
		return true;
	}
}
